package com.woniuxy.creditsystem.common.mapper;

import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.ProductInfo;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import org.apache.ibatis.annotations.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;


/**
 * 合作产品的增，该，查 mapper类  夏先生
 * */
@Mapper
public interface ProductMapper {


    /**
     * 修改用户--------有空在test条件中加上“xxx！=‘’”
     * @param productInfo
     * @return 受影响的行数
     */
    @Update("<script>" +
            "update product_info" +
            "<set>" +
            "<if test='p.productType != null'>" +
            " product_type = #{p.productType}," +
            "</if>" +
            "<if test='p.productName != null'>" +
            " product_name = #{p.productName}," +
            "</if>" +
            "<if test='p.ditchNums != null '>" +
            " ditch_nums= #{p.ditchNums}," +
            "</if>" +
            "<if test='p.productDescp != null'>" +
            " product_descp = #{p.productDescp}," +
            "</if>" +
            "<if test='p.refundType != null'>" +
            " refund_type = #{p.refundType}," +
            "</if>" +
            "<if test='p.periods  != null'>" +
            " periods = #{p.periods}," +
            "</if>" +
            "<if test='p.monthInterestRate != null'>" +
            " month_interest_rate = #{p.monthInterestRate}," +
            "</if>" +
            "<if test='p.handleCost != null'>" +
            " handle_cost = #{p.handleCost}," +
            "</if>" +
            "<if test='p.penaltyInterestRate != null'>" +
            " penalty_interest_rate = #{p.penaltyInterestRate}," +
            "</if>" +
            "<if test='p.endPerson != null'>" +
            "end_person = #{p.endPerson}," +
            "</if>" +
            "create_time=now()"+
            "</set>" +
            "where id = #{p.id}" +
            "</script>")
    int updaProductInfo(@Param("p")ProductInfo productInfo);



    /**
     *
     * @param productInfo
     * @return
     */
    @Insert("insert into product_info ( ditch_nums , product_nums , product_type,product_name,fk_channel_id,product_descp,product_status,create_time," +
            "refund_type,periods,month_interest_rate,handle_cost,penalty_interest_rate,end_person) " +
            "values(#{p.ditchNums},#{p.productNums},#{p.productType},#{p.productName},#{p.fkChannelId},#{p.productDescp},'已禁用',now(),#{p.refundType},#{p.periods}," +
            "#{p.monthInterestRate},#{p.handleCost},#{p.penaltyInterestRate},#{p.endPerson})")
    int saveProductInfo(@Param("p") ProductInfo productInfo);

    @Select("select id, product_name as productName ,product_status as productStatus,ditch_nums as ditchNums,refund_type as refundType,rate_compute_cycle as rateComputeCycle,periods,month_interest_rate as monthInterestRate,handle_cost handleCost,penalty_interest_rate as penaltyInterestRate,fk_channel_id  from product_info where id=#{id}")
    ProductInfo CfindByID(long id);

    /**
     * 语句较为复杂写在xml文件里面
     * @param request
     * @param productInfo
     * @return
     */
    List<ProductInfo> findAllByParams(@Param("p") PageRequest request, @Param("pro") ProductInfo productInfo);


    /**
     * 更改产品状态（已启用/已禁用）
     * @param productInfo
     * @return
     */
    int updaStatusById(ProductInfo productInfo);

    @Select("select id, product_nums productNums,product_name productName,product_type productType,product_descp productDescp," +
            "        product_status productStatus,ditch_nums ditchNums,end_person endPerson,create_time createTime," +
            "        refund_type refundType,rate_compute_cycle rateComputeCycle,periods,month_interest_rate monthInterestRate," +
            "        handle_cost handleCost,penalty_interest_rate penaltyInterestRate,  fk_channel_id fkChannelId" +
            "        from product_info where ditch_nums = #{id}")
    List<ProductInfo> findProductByDitchId(ProductInfo productInfo);
}
